package library.admin.controller;

import library.admin.domain.dto.PageDTO;
import library.admin.domain.po.User;
import library.admin.domain.query.UserPageQuery;
import library.admin.domain.request.SaveUserRequest;
import library.admin.domain.request.UpdateUserStatusRequest;
import library.admin.domain.vo.UserVO;
import library.admin.response.ResponseResult;
import library.admin.service.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户相关接口")
@RequiredArgsConstructor
@Slf4j
public class UsersController {
    private final IUsersService usersService;

    @GetMapping("login/{username}/{password}")
    @ApiOperation("登录")
    public ResponseResult<Null> login(@PathVariable String username,
                                      @PathVariable String password) {
        try {
            usersService.login(username, password);
            return ResponseResult.success();
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        }
    }
    
    @GetMapping("page")
    @ApiOperation("分页获取用户列表")
    public ResponseResult<PageDTO<UserVO>> queryUsersByPage(UserPageQuery pageQuery) {
        try {
            PageDTO<UserVO> pageDTO = usersService.queryUsersByPage(pageQuery);
            return ResponseResult.success(pageDTO);
        } catch (Exception e) {
            log.error("查询用户列表失败", e);
            return ResponseResult.fail("查询用户列表失败: " + e.getMessage());
        }
    }
    
    @GetMapping("{id}")
    @ApiOperation("获取用户详情")
    public ResponseResult<UserVO> getUserDetails(@PathVariable Long id) {
        try {
            UserVO userVO = usersService.getUserDetails(id);
            return ResponseResult.success(userVO);
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (Exception e) {
            log.error("获取用户详情失败", e);
            return ResponseResult.fail("获取用户详情失败: " + e.getMessage());
        }
    }
    
    @PostMapping
    @ApiOperation("添加用户")
    public ResponseResult<Long> saveUser(@RequestBody SaveUserRequest request) {
        try {
            Long id = usersService.saveUser(request);
            return ResponseResult.success(id);
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (Exception e) {
            log.error("添加用户失败", e);
            return ResponseResult.fail("添加用户失败: " + e.getMessage());
        }
    }
    
    @PutMapping("status")
    @ApiOperation("更新用户状态")
    public ResponseResult<Null> updateUserStatus(@RequestBody UpdateUserStatusRequest request) {
        try {
            boolean success = usersService.updateUserStatus(request);
            if (success) {
                return ResponseResult.success();
            } else {
                return ResponseResult.fail("更新用户状态失败");
            }
        } catch (IllegalArgumentException iae) {
            return ResponseResult.fail(iae.getMessage());
        } catch (Exception e) {
            log.error("更新用户状态失败", e);
            return ResponseResult.fail("更新用户状态失败: " + e.getMessage());
        }
    }
    
    @GetMapping("check-username")
    @ApiOperation("检查用户名是否存在")
    public ResponseResult<Boolean> existsByUsername(@RequestParam String username) {
        try {
            boolean exists = usersService.existsByUsername(username);
            return ResponseResult.success(exists);
        } catch (Exception e) {
            log.error("检查用户名失败", e);
            return ResponseResult.fail("检查用户名失败: " + e.getMessage());
        }
    }
    
    @GetMapping("check-email")
    @ApiOperation("检查电子邮箱是否存在")
    public ResponseResult<Boolean> existsByEmail(@RequestParam String email) {
        try {
            boolean exists = usersService.existsByEmail(email);
            return ResponseResult.success(exists);
        } catch (Exception e) {
            log.error("检查电子邮箱失败", e);
            return ResponseResult.fail("检查电子邮箱失败: " + e.getMessage());
        }
    }
}
